﻿using System;

namespace com.susi.util {
    public static class TypeExtensions {
        public static object CreatInstance(this Type type)
        {
            try {
                return Activator.CreateInstance(type);
            }
            catch {
                return null;
            }
        }

        public static T CreatInstance<T>(this Type type) where T : class
        {
            return (T) CreatInstance(type);
        }

        /// <summary>
        ///     检查该类型是否继承目标接口、类
        ///     <br>自动排除自身和要求被检查类是Class</br>
        /// </summary>
        public static bool IsClassAndExtend(this Type type, Type base1)
        {
            return type.IsClass && base1 != type && base1.IsAssignableFrom(type);
        }

        /// <summary>
        ///     检查该struct类型是否继承目标接口
        /// </summary>
        public static bool IsStructAndExtend(this Type type, Type base1)
        {
            return type.IsValueType && base1.IsAssignableFrom(type);
        }
    }
}